﻿using System.Collections.Generic;
using Assets.Scripts.Net;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.Net.Data.Reader;
using Assets.Scripts.UI.Panel;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Item
{
    public class ExerciseItem:UIBase<ExerciseItem>
    {
        public Button DeleteButton;
        public Button DetailButton;
        public Text NumText;
        public Text ExerciseName;
        public Text ExerciseType;

        private int oldScreenWidth;

        private QueInfo info { get; set; }

        protected override UIType UIType
        {
            get { return UIType.Item;}
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform, true);
            oldScreenWidth = Screen.width;
            FitResolution();
        }

        protected override void Update()
        {
            if (oldScreenWidth!=Screen.width)
            {
                FitResolution();
                oldScreenWidth = Screen.width;
            }
        }

        /// <summary>
        /// 适配分辨率
        /// </summary>
        private void FitResolution()
        {
            //0.5625 9/16
            transform.localScale = new Vector3(0.5625f / ((float)Screen.height / Screen.width), 1, 1);
        }

        public void Init(QueInfo queInfo,int num)
        {
            info = queInfo;
            NumText.text = num.ToString();
            if (queInfo.content.Length > 10)
            {
                ExerciseName.text = queInfo.content.Substring(0, 10) + "...";
            }
            else
            {
                ExerciseName.text = queInfo.content;
            }
            switch (queInfo.quetype)
            {
                case 0://单选
                    ExerciseType.text = "单选题";
                    break;                
                case 1://多选
                    ExerciseType.text = "多选题";
                    break;
                case 2://判断
                    ExerciseType.text = "判断题";
                    break;             
                case 3://填空
                    ExerciseType.text = "填空题";
                    break;
                case 4://简答
                    ExerciseType.text = "简答题";
                    break;
            }
            DeleteButton.onClick.RemoveAllListeners();
            DeleteButton.onClick.AddListener(() =>
            {
                Dictionary<string, object> dict = NetManager.Instance.GetDict();
                dict.Add("id",queInfo.id);
                NetManager.Instance.SendRequest(RequestType.RemoveError, dict, () =>
                {
                    //todo 删除数据 
                    UIWarpContentItem item = this.GetComponent<UIWarpContentItem>();
                    CollectPanel._Instance.DelItem(item.Index,queInfo.id);
                });
            }); 
            DetailButton.onClick.RemoveAllListeners();
            DetailButton.onClick.AddListener(() =>
            {                
                ExerciseDetailPanel panel= CreatePanel(PanelType.ExerciseDetailPanel).GetComponent<ExerciseDetailPanel>();
                panel.Init(queInfo,num-1);
            });
        }
    }
}